package com.neuedu.mblog.service;

import com.neuedu.mblog.dao.ChannelDao;
import com.neuedu.mblog.dao.CommentDao;
import com.neuedu.mblog.entiry.Channel;
import com.neuedu.mblog.entiry.Comment;
import com.neuedu.mblog.entiry.Post;
import com.neuedu.mblog.entiry.User;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目：mblog_java4
 * 创建时间：  2021-12-29   9:21
 * 作者 :jshand
 * 描述 :
 */
public class CommentService {
    private CommentDao commentDao = new CommentDao();


    /**
     * 查询集合不分页
     * @param request
     * @return
     */
    public List<Comment> selectList(HttpServletRequest request) throws SQLException {
        //
        String sql = " SELECT  id, author_id,  content, created, pid, post_id, STATUS  FROM  mto_comment ORDER BY created DESC  ";
        return commentDao.selectList(sql ,null);
    }




    public boolean deleteById(HttpServletRequest request) throws SQLException {
        String id = request.getParameter("id");
        return commentDao.delete(Integer.parseInt(id))>0;
    }



    public void pageList(HttpServletRequest request) throws SQLException, ClassNotFoundException {
        Integer pageNum = 1;
        Integer pageSize = 10;


        String strPageNum = request.getParameter("pageNum");
        String strPageSize = request.getParameter("pageSize");
        if(strPageNum !=null && !"".equals(strPageNum)){
            pageNum =  Integer.parseInt(strPageNum);
        }
        if(strPageSize !=null && !"".equals(strPageSize)){
            pageSize =  Integer.parseInt(strPageSize);
        }


        //当前登录的用户
        User user = (User) request.getSession().getAttribute("user");

        StringBuilder sql = new StringBuilder();
        sql.append("  SELECT 									");
        sql.append("    mto_comment.id,                       "  );
        sql.append("    mto_comment.author_id,                "  );
        sql.append("    mto_comment.content,                  "  );
        sql.append("    mto_comment.created,                  "  );
        sql.append("    mto_comment.pid,                      "  );
        sql.append("    mto_comment.post_id,                  "  );
        sql.append("    mto_comment.status ,                  "  );
        sql.append("    mto_post.title                        "  );
        sql.append("  FROM                                    "  );
        sql.append("    mto_comment                           "  );
        sql.append("    INNER JOIN mto_post                   "  );
        sql.append("  WHERE mto_comment.post_id = mto_post.id "  );
        sql.append("  and mto_comment.author_id = ?             "           );



        ArrayList paramList = new ArrayList();
        paramList.add(user.getId());

        //select count(1) from (sql)
        long total = commentDao.total(sql.toString() ,paramList.toArray());
        long pageCount = total%pageSize == 0?total/pageSize:(total/pageSize)+1;

        sql .append( " limit  ? ,?");
        paramList.add((pageNum-1) * pageSize);
        paramList.add(pageSize);

        List<Comment> pageList = commentDao.selectListWithPost(sql.toString(),paramList.toArray());

        //使用request共享数据
        request.setAttribute("list",pageList);
        request.setAttribute("total",total);
        request.setAttribute("pageNum",pageNum);
        request.setAttribute("pageSize",pageSize);
        request.setAttribute("pageCount",pageCount);

//        System.out.println("total = " + total);
//        System.out.println("pageCount = " + pageCount);
//        System.out.println("pageSize = " + pageSize);
//        System.out.println("pageNum = " + pageNum);
//        System.out.println("list = " + pageList);


    }
}
